home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6736 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  80 lines

  1. Path: sun001.spd.dsccc.com!spd!jmccarty
  2. From: jmccarty@spd.dsccc.com (Mike McCarty)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: volatile type
  5. Date: 14 Feb 1996 19:30:51 GMT
  6. Organization: DSC Communications Corporation, Plano, Texas USA
  7. Message-ID: <4ftd9b$as2@sun001.spd.dsccc.com>
  8. References: <4fro3j$jbi@service.polymtl.ca>
  9. NNTP-Posting-Host: aplo139.spd.dsccc.com
  10.  
  11. In article <4fro3j$jbi@service.polymtl.ca>,
  12. Yannick Heneault <fermi@info.polymtl.ca> wrote:
  13. )Can someone know information about this data type. A example is
  14. )welcome!
  15.  
  16. "volatile" is not a type. It is a storage class. It ranks with "static"
  17. in this respect. What it means to the compiler is that certain
  18. optimizations cannot be performed on this variable. Specifically, any
  19. optimizations which depend on the variable retaining its value between
  20. accesses is prohibited.
  21.  
  22. As an example of the type of optimization prohibited: (C source and
  23. output assembler)
  24.  
  25.  
  26.     if (x < 0) y = x;
  27.  
  28.         load    x
  29.         compare    0
  30.         jump    greater_or_equal,around
  31.  
  32.         load    x
  33.         store    y
  34.     around:
  35.  
  36. Many compilers would remove the second load of x, since the value of x
  37. is already in the processor. This optimization depends on x not changing
  38. values between accesses.
  39.  
  40. As an example of the intended use, consider a "variable" which is
  41. actually a memory mapped I/O port, say for a serial device. When looking
  42. for a character from the serial port, one reads the status register and
  43. looks for some bit turned on (or off) to indicate that a character is
  44. available for read.
  45.  
  46.     char    serial_get_char() {
  47.         unsigned char    *status = SERIAL_DEVICE_STATUS_ADDRESS,
  48.                 *data   = SERIAL_DEVICE_DATA_ADDRESS;
  49.  
  50.         while ((*status & CHAR_AVAILABLE) == 0)
  51.             ;
  52.         return *data;
  53.     }
  54.  
  55. The generated code might look like this:
  56.  
  57.     load    [status]
  58.     and    0x20
  59.     jump    not_zero,label_1000
  60. label_1001:
  61.     jump    label_1001
  62. label_1000:
  63.     load    [data]
  64.     return
  65.  
  66. The compiler has noticed that the load and compare are loop invariant
  67. and moved them outside the loop. By specifying "volatile", for the
  68. variable "status", the user forces the compiler to consider the
  69. variable might change between reads. Thus "label_1001" would be placed
  70. -before- the load from status, since its value is no longer loop invariant.
  71.  
  72. Another place where one might use this is where another context (task
  73. or interrupt, etc) might modify the variable.
  74.  
  75. Mike
  76. ----
  77. char *p="char *p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}
  78.  
  79. I don't speak for DSC.         <- They make me say that.
  80.